package com.rtx.wms.customer.grp.handler.asn;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rtx.api.annotation.DataSourceAnnotation;
import com.rtx.edi.model.api.bo.ApiMappingInfoBo;
import com.rtx.wms.customer.common.upload.UploadService;
import com.rtx.wms.customer.grp.upload.impl.GRPAsnUploadService;
import com.rtx.wms.std.asn.bean.ApiReceiptfinish;
import com.rtx.wms.std.asn.bean.Receipt;
import com.rtx.wms.std.asn.service.ApiReceiptfinishService;
import com.rtx.wms.std.asn.service.ReceiptService;
import com.rtx.wms.std.common.bean.WmsConstant;

import cn.hutool.core.util.StrUtil;

/**
 * 入库单回传
 */
@Primary
@Component
public class GrpReceiptReturnHandler {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ApiReceiptfinishService apiReceiptfinishService;

    @Autowired
    ReceiptService receiptService;
    
    @Autowired
    GRPAsnUploadService asnUploadService;
    
    @Autowired
    ApplicationContext applicationContext;
    
    

    @DataSourceAnnotation
    public void handlerOrdersReturn(String whseid,ApiMappingInfoBo bo){
        //获取入库单回传数据
        List<ApiReceiptfinish> ordersList = selectApiReceiptfinish("ris_edi");
        
        for(ApiReceiptfinish o : ordersList){
            //回传
        	UploadService service = (UploadService)applicationContext.getBean(bo.getApimappingcode());
        	service.uploadHandle(o);
            //更新状态
            updateApiReceiptfinish(whseid,o);
        }
    }

    /**
     * 更新
     * @param whseid
     * @param o
     */
    private void updateApiReceiptfinish(String whseid, ApiReceiptfinish o) {
        try {
            LambdaUpdateWrapper<ApiReceiptfinish> wrapper = Wrappers.lambdaUpdate();
            wrapper.eq(ApiReceiptfinish::getBatchid,o.getBatchid())
                    .set(ApiReceiptfinish::getEdiflag,o.getEdiflag())
                    .set(ApiReceiptfinish::getEdimsg,StrUtil.sub(o.getEdimsg(),0,200))
                    .set(ApiReceiptfinish::getRtxqueue,o.getRtxqueue() + 1);
            apiReceiptfinishService.updateByWrapper("ris_edi",wrapper);
            
            LambdaUpdateWrapper<Receipt> rwrapper = Wrappers.lambdaUpdate();
            rwrapper.eq(Receipt::getReceiptkey, o.getReceiptkey())
            .set(Receipt::getReceiptfinishflag, o.getEdiflag())
            .set(Receipt::getReturnfailedreason,StrUtil.sub(o.getEdimsg(),0,200));
            receiptService.updateByWrapper(whseid, rwrapper);
        } catch (Exception e) {
            logger.error("入库单：{}更新回传状态异常",o.getReceiptkey(),e);
        }
    }

    /**
     * 获取入库单回传数据
     * @param whseid
     * @return
     */
    public List<ApiReceiptfinish> selectApiReceiptfinish(String whseid){
        LambdaQueryWrapper<ApiReceiptfinish> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(ApiReceiptfinish::getEdiflag, Arrays.asList(WmsConstant.DEFAULT_FLAG,WmsConstant.FAIL_FLAG))
                    .lt(ApiReceiptfinish::getRtxqueue,10);
        List<ApiReceiptfinish> list = apiReceiptfinishService.selectApiReceiptfinishByWrapper(whseid,queryWrapper);
        return list;
    }
}
